
#include <stdio.h>      // needed on Linux system
#include <unistd.h>     // for getopt
#include <iostream>
#include "tinytest.h"
using namespace std;



int curr_lineno = 1;
char *curr_filename = "<stdin>"; // this name is arbitrary
FILE *fin;   // This is the file pointer from which the lexer reads its input.
YYSTYPE cool_yylval; 
//
//  cool_yylex() is the function produced by flex. It returns the next
//  token each time it is called.
//
extern int cool_yylex();

//extern void yyparse();


std::map<std::string, int> string_mp; //符号值
std::vector<std::string> string_table;//符号表

string token_to_string(int token){
	switch(token){
		case IF:
			return "IF";
		case THEN:
			return "THEN";
		case ELSE:
			return "ELSE";
		case END:
			return "END";
		case UNTIL:
			return "UNTIL";
		case READ:
			return "READ";
		case WRITE:
			return "WRITE";
		case NUMBER:
			return "NUMBER";
		case ASSIGN:
			return "ASSIGN";
		case EQ:
			return "EQ";
		case LT:
			return "GT";
		case PLUS:
			return "PLUS";
		case MINUS:
			return "MINUS";
		case TIMES:
			return "TIMES";
		case OVER:
			return "OVER";
		case LPAREN:
			return "LPAREN";
		case RPAREN:
			return "RPAREN";
		case ID:
			return "ID";
		case SEMI:
			return "SEMI";
		case ERROR:
			return "ERROR";

		default:
			return "UNKNOWN";

	}
}
// defined in utilities.cc
void dump_cool_token(ostream& out, int lineno, int token, YYSTYPE yylval){
	if(token==ID)
    	out<<lineno<<" "<<token_to_string(token)<<" "<<string_table[yylval.str_id]<<endl;
	else if(token==NUMBER)
		out<<lineno<<" "<<token_to_string(token)<<" "<<yylval.num<<endl;
	else
		out<<lineno<<" "<<token_to_string(token)<<endl;
};



int yywrap() {
    return 0;  // 默认行为：告诉解析器已经到达输入结尾
}

int main(int argc, char** argv) {
	int token;
    int optind=1;
	while (optind < argc) {
	    fin = fopen(argv[optind], "r");
	    if (fin == NULL) {
		cerr << "Could not open input file " << argv[optind] << endl;
		exit(1);
	    }
        curr_lineno = 1;

	    //
	    // Scan and print all tokens.
	    //
	    cout << "#name \"" << argv[optind] << "\"" << endl;
	    while ((token = cool_yylex()) != 0) {
		    dump_cool_token(cout, curr_lineno, token, cool_yylval);
	    }
	    fclose(fin);
	    optind++;
	}

    //yyparse();
	exit(0);
}



